package apresentacao.caminhoneiro;

import dao.caminhoneiro.AsyncTask_Buscar_Fretes;
import tcc.caminhoneiro.R;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;

public class Fragment_Buscar_Fretes extends Fragment{
	Spinner spCidadeOrigem, spCidadeDestino, spEstadoOrigem, spEstadoDestino;
	Button btBuscar;
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View view = inflater.inflate(R.layout.fragment_buscar_fretes, null);
		inicializar(view);
		return view;
	}

	
	OnClickListener eventoBuscar = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			AsyncTask_Buscar_Fretes async = new AsyncTask_Buscar_Fretes(spCidadeOrigem.getSelectedItem().toString(), spEstadoOrigem.getSelectedItem().toString(), spCidadeDestino.getSelectedItem().toString(), spEstadoDestino.getSelectedItem().toString(), getActivity());
			async.execute();
		}
	};
	
	private void inicializar(View view) {
		spCidadeOrigem 	= (Spinner) view.findViewById(R.id.buscar_fretes_spCidadeOrigem);
		spCidadeDestino = (Spinner) view.findViewById(R.id.buscar_fretes_spCidadeDestino);
		spEstadoOrigem 	= (Spinner) view.findViewById(R.id.buscar_fretes_spEstadoOrigem);
		spEstadoDestino = (Spinner) view.findViewById(R.id.buscar_fretes_spEstadoDestino);
		btBuscar 		= (Button) view.findViewById(R.id.buscar_fretes_btBuscar);
		
		initEstadoDestino();
		initEstadoOrigem();
		btBuscar.setOnClickListener(eventoBuscar);
		
		OnItemSelectedListener eventoEstadoOrigem = new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
				// TODO Auto-generated method stub
				setAdapterCidade(true, position);
			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {
				// TODO Auto-generated method stub
				
			}
			
		};
		spEstadoOrigem.setOnItemSelectedListener(eventoEstadoOrigem);
		
		OnItemSelectedListener eventoEstadoDestino = new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
				// TODO Auto-generated method stub
				setAdapterCidade(false, position);
			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {
				// TODO Auto-generated method stub
				
			}
			
		};
		spEstadoDestino.setOnItemSelectedListener(eventoEstadoDestino);		
	}
	
	public void setAdapterCidade(boolean isOrigem, int position){
		ArrayAdapter<CharSequence> adapter = null;
		switch (position) {
		case 0:{
			adapter = ArrayAdapter.createFromResource(getActivity(), R.array.Acre, android.R.layout.simple_spinner_item);
			break;	
		}
		case 1:{
			adapter = ArrayAdapter.createFromResource(getActivity(), R.array.Alagoas, android.R.layout.simple_spinner_item);
			break;	
		}
		case 2:{
			adapter = ArrayAdapter.createFromResource(getActivity(), R.array.Amapa, android.R.layout.simple_spinner_item);
			break;	
		}
		case 3:{
			adapter = ArrayAdapter.createFromResource(getActivity(), R.array.Amazonas, android.R.layout.simple_spinner_item);
			break;	
		}
		case 4:{
			adapter = ArrayAdapter.createFromResource(getActivity(), R.array.Bahia, android.R.layout.simple_spinner_item);
			break;	
		}
		case 5:{
			adapter = ArrayAdapter.createFromResource(getActivity(), R.array.Ceara, android.R.layout.simple_spinner_item);
			break;	
		}
		case 6:{
			adapter = ArrayAdapter.createFromResource(getActivity(), R.array.Distrito_Federal, android.R.layout.simple_spinner_item);
			break;	
		}
		case 7:{
			adapter = ArrayAdapter.createFromResource(getActivity(), R.array.Espirito_Santo, android.R.layout.simple_spinner_item);
			break;	
		}
		case 8:{
			adapter = ArrayAdapter.createFromResource(getActivity(), R.array.Goias, android.R.layout.simple_spinner_item);
			break;	
		}
		case 9:{
			adapter = ArrayAdapter.createFromResource(getActivity(), R.array.Maranhao, android.R.layout.simple_spinner_item);
			break;	
		}
		case 10:{
			adapter = ArrayAdapter.createFromResource(getActivity(), R.array.Mato_Grosso, android.R.layout.simple_spinner_item);
			break;	
		}
		case 11:{
			adapter = ArrayAdapter.createFromResource(getActivity(), R.array.Mato_Grosso_Do_Sul, android.R.layout.simple_spinner_item);	
			break;	
		}
		case 12:{
			adapter = ArrayAdapter.createFromResource(getActivity(), R.array.Minas_Gerais, android.R.layout.simple_spinner_item);
			break;	
		}
		case 13:{
			adapter = ArrayAdapter.createFromResource(getActivity(), R.array.Para, android.R.layout.simple_spinner_item);
			break;	
		}
		case 14:{
			adapter = ArrayAdapter.createFromResource(getActivity(), R.array.Paraiba, android.R.layout.simple_spinner_item);
			break;	
		}
		case 15:{
			adapter = ArrayAdapter.createFromResource(getActivity(), R.array.Parana, android.R.layout.simple_spinner_item);
			break;	
		}
		case 16:{
			adapter = ArrayAdapter.createFromResource(getActivity(), R.array.Pernambuco, android.R.layout.simple_spinner_item);
			break;	
		}
		case 17:{
			adapter = ArrayAdapter.createFromResource(getActivity(), R.array.Piaui, android.R.layout.simple_spinner_item);
			break;	
		}
		case 18:{
			adapter = ArrayAdapter.createFromResource(getActivity(), R.array.Rio_De_Janeiro, android.R.layout.simple_spinner_item);
			break;	
		}
		case 19:{
			adapter = ArrayAdapter.createFromResource(getActivity(), R.array.Rio_Grande_Do_Norte, android.R.layout.simple_spinner_item);
			break;	
		}
		case 20:{
			adapter = ArrayAdapter.createFromResource(getActivity(), R.array.Rio_Grande_Do_Sul, android.R.layout.simple_spinner_item);
			break;	
		}
		case 21:{
			adapter = ArrayAdapter.createFromResource(getActivity(), R.array.Rondonia, android.R.layout.simple_spinner_item);
			break;	
		}
		case 22:{
			adapter = ArrayAdapter.createFromResource(getActivity(), R.array.Roraima, android.R.layout.simple_spinner_item);
			break;	
		}
		case 23:{
			adapter = ArrayAdapter.createFromResource(getActivity(), R.array.Santa_Catarina, android.R.layout.simple_spinner_item);
			break;	
		}
		case 24:{
			adapter = ArrayAdapter.createFromResource(getActivity(), R.array.Sao_Paulo, android.R.layout.simple_spinner_item);
			break;	
		}
		case 25:{
			adapter = ArrayAdapter.createFromResource(getActivity(), R.array.Sergipe, android.R.layout.simple_spinner_item);
			break;	
		}
		case 26:{
			adapter = ArrayAdapter.createFromResource(getActivity(), R.array.Tocantins, android.R.layout.simple_spinner_item);
			break;	
		}
		}
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		if(isOrigem){
			spCidadeOrigem.setAdapter(adapter);
		}else{
			spCidadeDestino.setAdapter(adapter);
		}
	}
	
	public void initCidadeOrigem(){
		ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getActivity(), R.array.veiculos, android.R.layout.simple_spinner_item);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	    spCidadeOrigem.setAdapter(adapter);
	}
	
	public void initCidadeDestino(){
		ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getActivity(), R.array.veiculos, android.R.layout.simple_spinner_item);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	    spCidadeDestino.setAdapter(adapter);
	}
	
	public void initEstadoOrigem(){
		ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getActivity(), R.array.estados, android.R.layout.simple_spinner_item);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	    spEstadoOrigem.setAdapter(adapter);
	}
	
	public void initEstadoDestino(){
		ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getActivity(), R.array.estados, android.R.layout.simple_spinner_item);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	    spEstadoDestino.setAdapter(adapter);
	}
}
